package sz.szsmk.citizencard.ui.adapter;

import android.os.Bundle;
import androidx.fragment.app.FragmentActivity;
import androidx.recyclerview.widget.RecyclerView;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.alibaba.android.vlayout.DelegateAdapter;
import com.alibaba.android.vlayout.LayoutHelper;
import com.alibaba.android.vlayout.layout.DefaultLayoutHelper;

import org.greenrobot.eventbus.EventBus;

import java.util.List;

import sz.szsmk.citizencard.DaggerApplication;
import sz.szsmk.citizencard.R;
import sz.szsmk.citizencard.databinding.OrderViewBinding;
import sz.szsmk.citizencard.model.network.bean.response.QueryOrderCountRes;
import sz.szsmk.citizencard.model.network.dzbean.res.MessageEvent;
import sz.szsmk.citizencard.util.ACache;
import sz.szsmk.citizencard.util.Constant;
import sz.szsmk.citizencard.util.JumpUtils;

/**
 * Copyright (c)2019
 * author: zdh
 * created on: 2019/1/7 17:38
 * description:
 */
public class OrderAdapter extends DelegateAdapter.Adapter<OrderAdapter.OrderHolder> {

    private FragmentActivity mContext;
    private LayoutHelper mDefaultLayoutHelper;
    private QueryOrderCountRes viewData;

    public OrderAdapter(FragmentActivity activity, DefaultLayoutHelper defaultLayoutHelper) {
        mContext = activity;
        mDefaultLayoutHelper = defaultLayoutHelper;
    }

    @Override
    public int getItemViewType(int position) {
        return R.layout.order_view;
    }

    @Override
    public LayoutHelper onCreateLayoutHelper() {
        return mDefaultLayoutHelper;
    }

    @Override
    public OrderHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        return OrderHolder.creat(parent);
    }

    @Override
    public void onBindViewHolder(OrderHolder holder, int position) {
        loadItemData(position, holder.binding);
    }

    private void loadItemData(int position, OrderViewBinding binding) {
        final String access_token = ACache.get(DaggerApplication.getIns()).getAsString("access_token");
        if (viewData != null) {
            List<QueryOrderCountRes.OrderCountListBean> orderCountList = viewData.getOrderCountList();
            if (orderCountList != null) {
                for (int i = 0; i < orderCountList.size(); i++) {

                    QueryOrderCountRes.OrderCountListBean tempBean = orderCountList.get(i);
                    switch (tempBean.getOrderType()){
                        case "0":
                            //  0是全部
                            break;
                        case "1":
                            if(tempBean.getCount()!=0){
                                binding.dfkTv.setText("待付款(" + tempBean.getCount() + ")");
                            }
                            break;
                        case "2":
                            if(tempBean.getCount()!=0){
                                binding.yfkTv.setText("已付款("+tempBean.getCount() + ")");
                            }
                            break;
                        case "3":
                            if(tempBean.getCount()!=0){
                                binding.dxkTv.setText("待写卡(" + tempBean.getCount() + ")");
                            }
                            break;
                        case "4":
                            if(tempBean.getCount()!=0){
                                binding.ywcTv.setText("已完成("+ tempBean.getCount() + ")");
                            }
                            break;
                    }
                }
                binding.orderDfk.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Bundle bundle = new Bundle();
                        bundle.putString("url", Constant.BASE_URL + "app-front-end/#/myOrder/all?page_type=2&access_token=" + access_token);
                        bundle.putString(Constant.TITLE, "我的订单");
                        JumpUtils.jumptoNatvie(mContext, "webView", 0, bundle);
                    }
                });

                binding.orderYfk.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Bundle bundle = new Bundle();
                        bundle.putString("url", Constant.BASE_URL + "app-front-end/#/myOrder/all?page_type=3&access_token=" + access_token);
                        bundle.putString(Constant.TITLE, "我的订单");
                        JumpUtils.jumptoNatvie(mContext, "webView", 0, bundle);

                    }
                });
                binding.orderDxk.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Bundle bundle = new Bundle();
                        bundle.putString("url", Constant.BASE_URL + "app-front-end/#/myOrder/all?page_type=4&access_token=" + access_token);
                        bundle.putString(Constant.TITLE, "我的订单");
                        JumpUtils.jumptoNatvie(mContext, "webView", 0, bundle);

                    }
                });
                binding.orderYwc.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Bundle bundle = new Bundle();
                        bundle.putString("url", Constant.BASE_URL + "app-front-end/#/myOrder/all?page_type=5&access_token=" + access_token);
                        bundle.putString(Constant.TITLE, "我的订单");
                        JumpUtils.jumptoNatvie(mContext, "webView", 0, bundle);

                    }
                });
                binding.orderAll.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                        Bundle bundle = new Bundle();
                        bundle.putString("url", Constant.BASE_URL + "app-front-end/#/myOrder/all?page_type=1&access_token=" + access_token);
                        bundle.putString(Constant.TITLE, "我的订单");
                        JumpUtils.jumptoNatvie(mContext, "webView", 0, bundle);

                    }
                });
            }

        }

        binding.orderDfk.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Bundle bundle = new Bundle();
                bundle.putString("url", Constant.BASE_URL + "app-front-end/#/myOrder/all?page_type=2&access_token=" + access_token);
                bundle.putString(Constant.TITLE, "我的订单");
                JumpUtils.jumptoNatvie(mContext, "webView", 0, bundle);
                //埋点 到主界面 发送数据
                MessageEvent message = new MessageEvent(MessageEvent.CODE_USERBEHAVIOR);
                message.setData(46);
                EventBus.getDefault().post(message);
            }
        });

        binding.orderYfk.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Bundle bundle = new Bundle();
                bundle.putString("url", Constant.BASE_URL + "app-front-end/#/myOrder/all?page_type=3&access_token=" + access_token);
                bundle.putString(Constant.TITLE, "我的订单");
                JumpUtils.jumptoNatvie(mContext, "webView", 0, bundle);
                //埋点 到主界面 发送数据
                MessageEvent message = new MessageEvent(MessageEvent.CODE_USERBEHAVIOR);
                message.setData(46);
                EventBus.getDefault().post(message);
            }
        });
        binding.orderDxk.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Bundle bundle = new Bundle();
                bundle.putString("url", Constant.BASE_URL + "app-front-end/#/myOrder/all?page_type=4&access_token=" + access_token);
                bundle.putString(Constant.TITLE, "我的订单");
                JumpUtils.jumptoNatvie(mContext, "webView", 0, bundle);
                //埋点 到主界面 发送数据
                MessageEvent message = new MessageEvent(MessageEvent.CODE_USERBEHAVIOR);
                message.setData(46);
                EventBus.getDefault().post(message);
            }
        });
        binding.orderYwc.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Bundle bundle = new Bundle();
                bundle.putString("url", Constant.BASE_URL + "app-front-end/#/myOrder/all?page_type=5&access_token=" + access_token);
                bundle.putString(Constant.TITLE, "我的订单");
                JumpUtils.jumptoNatvie(mContext, "webView", 0, bundle);
                //埋点 到主界面 发送数据
                MessageEvent message = new MessageEvent(MessageEvent.CODE_USERBEHAVIOR);
                message.setData(46);
                EventBus.getDefault().post(message);
            }
        });
        binding.orderAll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.i("access_token","access_token:"+access_token);
                Bundle bundle = new Bundle();
                bundle.putString("url", Constant.BASE_URL + "app-front-end/#/myOrder/all?page_type=1&access_token=" + access_token);
                bundle.putString(Constant.TITLE, "我的订单");
                JumpUtils.jumptoNatvie(mContext, "webView", 0, bundle);
                //埋点 到主界面 发送数据
                MessageEvent message = new MessageEvent(MessageEvent.CODE_USERBEHAVIOR);
                message.setData(46);
                EventBus.getDefault().post(message);
            }
        });
    }

    @Override
    public int getItemCount() {
        return 1;
    }

    public void setViewData(QueryOrderCountRes viewData) {
        this.viewData = viewData;
    }

    public static class OrderHolder extends RecyclerView.ViewHolder {
        private OrderViewBinding binding;

        public static OrderHolder creat(ViewGroup parent) {
            OrderViewBinding binding = OrderViewBinding.inflate(LayoutInflater.from(parent.getContext()), parent, false);
            return new OrderHolder(binding);
        }

        public OrderHolder(OrderViewBinding binding) {
            super(binding.getRoot());
            this.binding = binding;
        }
    }

}
